<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-scrollX-info-widget" :style="getStyle('area')">
      <template v-if="true">
        <div class="left" v-if="shownLeft" :style="getStyle('left')">
          <template v-if="optionsInfo['icon']['shown']">
            <Icon
                class="icon"
                :style="getStyle('icon')"
                :name="optionsInfo['icon']['type'] || 'clock-o'"
            />
          </template>
          <div class="date" :style="getStyle('date')" v-if="optionsInfo['dateTime']['shown']">
            {{ date }}
          </div>
          <div class="time" :style="getStyle('time')" v-if="optionsInfo['dateTime']['timeShown']">
            {{ time }}
          </div>
          <div class="week" v-if="false">{{ getWeekInfo(week) }}</div>
        </div>
        <div class="right" :key="uuid" :style="getStyle('content')">
          <ScrollEditX :content="optionsInfo.content.value"/>
        </div>
      </template>
    </div>
  </CompOperate>
</template>

<script>
import dayjs from "dayjs";
import {v4 as uuid} from 'uuid';
import {Icon, NoticeBar} from 'vant';
import emitter from "@/utils/designer/emitter";
import {getWeek, pxToVw} from '@/utils/common'
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import NoticeBarInfo from '@/components/public/noticeBar'
import ScrollEditX from "@c/public/seamlessScroll/ScrollEditX.vue";

export default {
  name: "scene-scrollX-info-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  props: {
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    selectedWidget: Object,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    noticeContent: {
      type: String,
      default: '',
    },
    enableScroll: {
      type: Boolean,
      default: false,
    },
  },

  components: {
    ScrollEditX,
    Icon,
    NoticeBar,
    NoticeBarInfo,
    CompOperate,
  },

  data() {
    return {
      week: '',
      list: [],
      uuid: uuid(),
      optionsInfo: Object(),
      date: dayjs().format('YYYY年MM月DD日'),
      time: dayjs().format('HH:mm'),
    }
  },

  computed: {

    // 获取设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Object();
        let Obj = Object();
        switch (val) {
          case 'area':
            if (info?.background) {
              Reflect.set(Obj, 'backgroundColor', `${(info.background)}`);
            } else {
              Reflect.set(Obj, 'backgroundColor', `transparent`);
            }
            Reflect.set(Obj, 'height', pxToVw(info.viewHeight));
            Reflect.set(Obj, 'marginTop', pxToVw(info.marginTop));
            return Obj;
          case 'left':
            Reflect.set(Obj, 'backgroundColor', `${(info.iconTime.background)}`);
            return Obj;
          case 'icon':
            Reflect.set(Obj, 'fontSize', pxToVw(info.icon.fontSize));
            Reflect.set(Obj, 'color', `${(info.icon.fontColor)}`);
            return Obj;
          case 'date':
          case 'time':
            Reflect.set(Obj, 'fontSize', pxToVw(info.dateTime.fontSize));
            Reflect.set(Obj, 'color', `${(info.dateTime.fontColor)}`);
            Reflect.set(Obj, 'fontWeight', `${(info.dateTime.fontWeight)}`);
            return Obj;
          case 'content':
            Reflect.set(Obj, 'background', `${(info.content.background)}`);
            Reflect.set(Obj, 'fontSize', pxToVw(info.content.fontSize));
            Reflect.set(Obj, 'color', (info.content.fontColor));
            Reflect.set(Obj, 'fontWeight', (info.content.fontWeight));
            return Obj;
        }
      }
    },

    timeType() {
      let {dateTime} = this.optionsInfo
      return dateTime.value || 'YYYY/MM/DD'
    },

    // 获取日期
    getWeekInfo() {
      return function (info) {
        console.log(info)
        return getWeek(info)
      }
    },

    shownLeft() {
      let {icon, dateTime} = this.optionsInfo;
      return icon['shown'] || dateTime['shown'] || dateTime['timeShown'];
    }

  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "TipsInfo":
          console.log(`温馨提示-数据处理逻辑-${data}`)
          break
      }
    },

    // 获取时间
    updateTime(val) {
      let {dateTime} = (this.optionsInfo);
      if (!!val) {
        let msg = JSON.parse(val);
        if (!!msg && !!(msg.timestamp)) {
          let timestamp = dayjs.unix(msg.timestamp);
          [this.week, this.date, this.time] = dayjs(timestamp).format(`d,${dateTime.value},HH:mm`).split(',');
        } else {
          [this.week, this.date, this.time] = dayjs().format(`d,${dateTime.value},HH:mm`).split(',');
        }
      } else {
        [this.week, this.date, this.time] = dayjs().format(`d,${dateTime.value},HH:mm`).split(',');
      }
    },
  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        this.uuid = uuid();
        let key = val.options.config
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    },

    // 监听时间更新
    timeType: {
      handler: function (val) {
        if (val) {
          this.updateTime()
        }
      },
      deep: true,
      immediate: true
    },
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-scrollX-info-widget {
  background-color: #fff;
  color: #333;
  height: 81px;
  font-size: 28px;
  width: 100%;
  box-sizing: border-box;
  display: flex;
  flex-direction: row;
  overflow: hidden;

  .left {
    white-space: nowrap;
    padding: 8px 30px 8px 18px;
    color: white;
    box-sizing: border-box;
    overflow: hidden;
    line-height: normal;
    flex-shrink: 0;
    display: flex;
    flex-direction: row;
    align-self: center;
    align-items: center;
    font-size: 60px;

    .icon {
      padding-right: 16px;
    }

    .date {
      padding-right: 20px;
    }
  }

  .right {
    display: flex;
    align-self: center;
    align-items: center;
    overflow: hidden;
    flex-grow: 1;
    height: 100%;
    padding: 0 18px 0 26px;

    :deep(.st-inner) {
      margin: inherit;
    }
  }
}
</style>
